﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using Comm;
using BusinessFacade;
using Entities;

namespace MedicareNoteManagerWeb.Web
{
    public partial class NoteMouble : BasePage
    {
        nm_NoteMouble_Model model1 = new nm_NoteMouble_Model();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                model1 = (nm_NoteMouble_Model)Session["NotemessageModel"];
                BindDrop();
                LoadList();
                lbl_Message.Text = "";
            }
        }
        /// <summary>
        /// 绑定区域
        /// </summary>
        public void BindDrop()
        {
            FillDropDownList("GetDrop", "AreaID", "AreaName", "请选择区域", this.FindDropArea);
        }

        /// <summary>
        /// 绑定表
        /// </summary>
        public void LoadList()
        {
            Busi_nm_NoteMouble users = new Busi_nm_NoteMouble();
            DataTable dt = users.GetList(txt_FindUser.Text.Trim(), int.Parse(FindDropArea.SelectedValue));
            Grd_User.DataSource = dt;
            Grd_User.DataBind();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grd_User_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Grd_User.EditIndex = e.NewEditIndex;
            LoadList();
        }


        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grd_User_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            Grd_User.EditIndex = -1;
            LoadList();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            LoadList();
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grd_User_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了Go按钮
                TextBox txtNewPageIndex = null;

                //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    //得到text控件
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }
            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            //得到新的值
            theGrid.PageIndex = newPageIndex;

            //重新绑定
            LoadList();
        }

        /// <summary>
        /// 删除 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grd_User_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ID = Convert.ToInt32(Grd_User.DataKeys[e.RowIndex].Values[0].ToString());
            try
            {
                Busi_nm_NoteMouble.Delete(ID);
                MessageBox.ShowDialog(Page, "删除成功！", "success");
                LoadList();
            }
            catch (Exception ex)
            {
                MessageBox.ShowDialog(Page, "删除失败！" + ex.Message, "error");
                LoadList();
            }
        }
    }
}